package com.usian.common.intercepton;

import com.usian.utils.threadlocal.ThreadLocalUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServiceInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
        String userId = request.getHeader("userId");
        //执行从headre头部获取userId
        //2.将我们从头部获取的userId赋值到工具类里面
        if (userId!=null && !userId.isEmpty())ThreadLocalUtils.setUser(Integer.parseInt(userId));
        if (request.getHeader("refreshToken")!=null){
            response.addHeader("refreshToken",request.getHeader("refreshToken"));
        }
        return true;
    }
}
